#!/usr/bin/env python
#coding=utf-8
import json
import requests

s = requests.session()
s.keep_alive = False

def login(url, payload):
	login = url["passportHub"]["url"] + url["passportHub"]["login"]
	print(login)
	response = requests.request("POST", login, data=payload)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	user = {
			"token": result["data"]["token"],
			"uid" : str(result["data"]["user"]["uid"])
		}
	return user

def registermEos(url, headers):
	register = url["iOTHub"]["url"] + url["iOTHub"]["mEosregister"]
	print(register)
	response = requests.request("POST", register, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def ideCreateCloudList(url, headers, payload):
	idecreatecloudlist = url["iOTHub"]["url"] + url["iOTHub"]["idecreatecloudlist"]
	print(idecreatecloudlist)
	response = requests.request("POST", idecreatecloudlist, data=payload, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def mEosGetCloudList(url, headers, listname):
	mEosgetcloudlist = ""
	if listname == 0:
		mEosgetcloudlist = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetcloudlist"] + "?listName="
	elif listname == -1:
		mEosgetcloudlist = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetcloudlist"]
	else:
		mEosgetcloudlist = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetcloudlist"] + "?listName=" + listname
	print(mEosgetcloudlist)
	response = requests.request("GET", mEosgetcloudlist, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def idePostCloudList(url, headers, payload):
	idepostcloudlist = url["iOTHub"]["url"] + url["iOTHub"]["idepostcloudlist"]
	print(idepostcloudlist)
	response = requests.request("POST", idepostcloudlist, data=payload, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def meosPostCloudList(url, headers, payload):
	mEospostcloudlist = url["iOTHub"]["url"] + url["iOTHub"]["mEospostcloudlist"]
	print(mEospostcloudlist)
	response = requests.request("POST", mEospostcloudlist, data=payload, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def ideGetCloudList(url, headers, listname):
	idegetcloudlist = url["iOTHub"]["url"] + url["iOTHub"]["idegetcloudlist"] + "?listName=" + listname
	print(idegetcloudlist)
	response = requests.request("GET", idegetcloudlist, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def ideGetCloudListLen(url, headers, listname):
	idegetcloudlistlen = url["iOTHub"]["url"] + url["iOTHub"]["idegetcloudlistlen"] + "?listName=" + listname
	print(idegetcloudlistlen)
	response = requests.request("GET", idegetcloudlistlen, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result	

def ideGetCloudListItemByIndex(url, headers, parameters):
	idegetCloudListItemByIndex = url["iOTHub"]["url"] + url["iOTHub"]["idegetCloudListItemByIndex"] + "?listName=" + parameters["listname"] + "&index=" + parameters["index"] + "&type=" + parameters["type"]
	print(idegetCloudListItemByIndex)
	response = requests.request("GET", idegetCloudListItemByIndex, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def mEosGetCloudListLen(url, headers, listname):
	mEosgetcloudlistlen = ""
	if listname == 0:
		mEosgetcloudlistlen = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetcloudlistlen"] + "?listName="
	elif listname == -1:
		mEosgetcloudlistlen = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetcloudlistlen"]
	else:
		mEosgetcloudlistlen = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetcloudlistlen"] + "?listName=" + listname
	print(mEosgetcloudlistlen)
	response = requests.request("GET", mEosgetcloudlistlen, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def mEosGetCloudListbyIndex(url, headers, parameters):
	mEosgetCloudListItemByIndex = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetCloudListItemByIndex"]
	if parameters["listname"] == None:
		mEosgetCloudListItemByIndex = mEosgetCloudListItemByIndex + "?index=" + parameters["index"] + "&type=" + parameters["type"]
	if parameters["index"] == None:
		mEosgetCloudListItemByIndex = mEosgetCloudListItemByIndex + "?listName=" + parameters["listname"] + "&type=" + parameters["type"]
	if parameters["type"] == None:
		mEosgetCloudListItemByIndex = mEosgetCloudListItemByIndex + "?listName=" + parameters["listname"] + "&index=" + parameters["index"]

	if parameters["listname"] != None and parameters["index"] != None and parameters["type"] != None:
		mEosgetCloudListItemByIndex = url["iOTHub"]["url"] + url["iOTHub"]["mEosgetCloudListItemByIndex"] + "?listName=" + parameters["listname"] + "&index=" + parameters["index"] + "&type=" + parameters["type"]
	print(mEosgetCloudListItemByIndex)
	response = requests.request("GET", mEosgetCloudListItemByIndex, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result


def ideRemoveCloudList(url, headers, payload):
	ideremovecloudlist = url["iOTHub"]["url"] + url["iOTHub"]["ideremovecloudlist"]
	print(ideremovecloudlist)
	response = requests.request("POST", ideremovecloudlist, data=payload, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result

def ideGetAllCloudList(url, headers):
	idegetallcloudlistname = url["iOTHub"]["url"] + url["iOTHub"]["idegetallcloudlistname"]
	print(idegetallcloudlistname)
	response = requests.request("GET", idegetallcloudlistname, headers=headers)
	result = json.loads(response.text)
	print(result)
	print(result["message"])
	return result





